import matplotlib.pyplot as plt
from matplotlib import colors
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import cv2


def show_colorarea(path):
    img = cv2.imread(path)
    # cv2.imshow('img', img)
    # cv2.waitKey()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv_img)
    fig = plt.figure(figsize=(8, 6), dpi=80)
    axis = fig.add_subplot(1, 1, 1, projection="3d")

    # 像素颜色设置
    pixel_colors = img.reshape((np.shape(img)[0] * np.shape(img)[1], 3))
    # 归一化
    norm = colors.Normalize(vmin=1., vmax=1.)
    norm.autoscale(pixel_colors)
    # 转换为list
    pixel_colors = norm(pixel_colors).tolist()
    # 显示三维散点图
    axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors, marker='.')
    axis.set_xlabel('hue')
    axis.set_ylabel('saturation')
    axis.set_zlabel('value')
    plt.show()


def change_color_dark(path):
    img = cv2.imread(path)
    # cv2.imshow('img', img)
    # cv2.waitKey()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    # light_white = (0, 0, 200)
    # dark_white = (145, 60, 255)
    light_blue = (45, 0, 30)
    dark_blue = (105, 300, 300)
    # white_mask = cv2.inRange(hsv_img, light_white, dark_white)
    blue_mask = cv2.inRange(hsv_img, light_blue, dark_blue)
    # mask = white_mask+blue_mask
    mask = blue_mask


    # mask = cv2.cvtColor(mask, cv2.COLOR_HSV2RGB)
    # mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
    mask = cv2.bitwise_not(mask)
    # plt.subplot(121)
    # plt.imshow(mask)
    result = cv2.bitwise_and(img, img, mask=mask)
    result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    # cv2.imshow('result', result)
    # cv2.waitKey(0)
    # 显示效果
    # plt.subplot(121)
    # plt.imshow(mask, cmap="gray")
    # plt.subplot(122)
    # plt.imshow(result)
    # plt.show()
    # mask = cv2.bitwise_not(mask)
    # result = cv2.bitwise_and(img, img, mask=mask)
    """contours, hierachy = cv2.findContours(
        image=mask,
        mode=cv2.RETR_EXTERNAL,
        method=cv2.CHAIN_APPROX_NONE,
        hierarchy=None,
        offset=None)
    for contour in contours:
        img = cv2.fillPoly(img, contour, 0)"""
    # plt.imshow(result)
    # plt.show()

    # 存储
    return result

def change_color_light(path):
    img = cv2.imread(path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    # light_white = (0, 0, 200)
    # dark_white = (145, 60, 255)
    light_blue = (45, 0, 30)
    dark_blue = (105, 300, 300)
    blue_mask = cv2.inRange(hsv_img, light_blue, dark_blue)
    mask = blue_mask
    # mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
    # mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
    # print(mask.shape, img.shape)
    # print(mask.dtype, img.dtype)
    [b, g, r] = cv2.split(img)
    # cv2.imshow('mask', mask)
    # cv2.waitKey(0)
    resultb = cv2.bitwise_or(b, mask)
    resultg = cv2.bitwise_or(g, mask)
    resultr = cv2.bitwise_or(r, mask)
    result = cv2.merge([resultb, resultg, resultr])
    result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
    # cv2.imshow('result', result)
    # cv2.waitKey(0)
    return result

if __name__ == '__main__':

    path = r'E:\2021homework\cv_masks\yolo\masks_data\IMFD\01000\01001_Mask_Mouth_Chin.jpg'
    # show_colorarea(path)
    # img = change_color_dark(path)
    img = change_color_light(path)
